package com.example.mall.web.controller;

import com.example.mall.domain.ProductPromotions;
import com.example.mall.dto.ProductPromotionsDto;
import com.example.mall.exception.ErrorEnumCode;
import com.example.mall.exception.GlobalCustomerException;
import com.example.mall.query.PageDTO;
import com.example.mall.query.ProductPromotionsQuery;
import com.example.mall.result.JSONResult;
import com.example.mall.service.IProductPromotionsService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/productPromotions")
public class ProductPromotionsController {

    @Autowired
    public IProductPromotionsService productPromotionsService;

    /**
     * 分页查询商品促销活动表信息
     * @param productPromotionsQuery
     * @return
     */
    @GetMapping("queryPage")
    @ApiOperation(value = "分页查询商品促销活动表信息",notes = "分页查询商品促销活动表信息",httpMethod = "GET")
    public JSONResult queryPage(ProductPromotionsQuery productPromotionsQuery){
        PageDTO<ProductPromotionsDto> productPromotionsDtoPageDTO = productPromotionsService.queryPage(productPromotionsQuery);
        return JSONResult.success("查询成功",productPromotionsDtoPageDTO);
    }

    /**
     * 根据id查询商品促销活动表信息
     * @param promotionId
     * @return
     */
    @GetMapping("query/{promotionId}")
    @ApiOperation(value = "根据id查询商品促销活动表信息",notes = "根据id查询商品促销活动表信息",httpMethod = "GET")
    public JSONResult query(@PathVariable("promotionId")Long promotionId){
        //1.根据id查询用户信息
        if (promotionId == null){
            throw new GlobalCustomerException(ErrorEnumCode.PROMOTIONID_NOT_ID);
        }
        ProductPromotions productPromotions = productPromotionsService.getById(promotionId);
        return JSONResult.success("查询成功",productPromotions);
    }

    /**
     * 根据id查询商品促销活动表信息
     * @param promotionId
     * @return
     */
    @GetMapping("queryProductPromotionsById/{promotionId}")
    @ApiOperation(value = "根据id查询商品促销活动表信息",notes = "根据id查询商品促销活动表信息",httpMethod = "GET")
    public JSONResult queryProductPromotionsById(@PathVariable("promotionId")Long promotionId){
        //1.根据id查询用户信息
        if (promotionId == null){
            throw new GlobalCustomerException(ErrorEnumCode.PROMOTIONID_NOT_ID);
        }
        ProductPromotions productPromotions = productPromotionsService.queryProductPromotionsById(promotionId);
        return JSONResult.success("查询成功",productPromotions);
    }

    /**
     * 查询全部商品促销活动表信息
     * @param
     * @return
     */
    @GetMapping("queryProductPromotionsList")
    @ApiOperation(value = "查询全部商品促销活动表信息",notes = "查询全部商品促销活动表信息",httpMethod = "GET")
    public JSONResult queryProductPromotionsList(){
        List<ProductPromotions> productPromotions = productPromotionsService.queryProductPromotionsList();
        return JSONResult.success("查询成功",productPromotions);
    }

    /**
     * 新增商品促销活动表
     * @param productPromotionsDto
     * @return
     */
    @PostMapping("insert")
    @ApiOperation(value = "新增商品促销活动表",notes = "新增商品促销活动表",httpMethod = "POST")
    public JSONResult insert(@RequestBody ProductPromotionsDto productPromotionsDto){
        productPromotionsService.insert(productPromotionsDto);
        return JSONResult.success("保存成功");
    }

    /**
     * 修改商品促销活动表信息
     * @param productPromotionsDto
     * @return
     */
    @PostMapping("update")
    @ApiOperation(value = "修改商品促销活动表信息",notes = "修改商品促销活动表信息",httpMethod = "POST")
    public JSONResult update(@RequestBody ProductPromotionsDto productPromotionsDto){
        productPromotionsService.update(productPromotionsDto);
        return JSONResult.success("修改成功");
    }

    /**
     * 删除商品促销活动表信息
     * @param promotionId
     * @return
     */
    @PostMapping("delete/{promotionId}")
    @ApiOperation(value = "删除商品促销活动表信息",notes = "删除商品促销活动表信息",httpMethod = "POST")
    public JSONResult delete(@PathVariable("promotionId") Long promotionId) {
        productPromotionsService.delete(promotionId);
        return JSONResult.success("删除成功");
    }

    /**
     * 批量删除商品促销活动表信息
     * @param promotionIds
     * @return
     */
    @PostMapping("deleteBatch")
    @ApiOperation(value = "批量删除商品促销活动表信息",notes = "批量删除商品促销活动表信息",httpMethod = "POST")
    public JSONResult deleteAll(@RequestBody List<Long> promotionIds) {
        productPromotionsService.deleteBatch(promotionIds);
        return JSONResult.success("批量删除成功");
    }
}
